<?php
// +----------------------------------------------------------------------
// | 作 者： songyb <1371626452@qq.com>
// +----------------------------------------------------------------------
// | 创建日期：2015-06-16
// +----------------------------------------------------------------------
// | 文件描述：项目全局公用函数
// +----------------------------------------------------------------------
// | 升级记录：
// +----------------------------------------------------------------------

/**
 * 获取当前登录管理员信息
 * 
 * @return NULL
 */
function get_admin_info()
{
    $data['userid'] = cookie('userid');
    $data['user_name'] = cookie('user_name');
    $data['user_real_name'] = cookie('user_real_name');
    $data['type'] = cookie('type');
    $data['area_code'] = cookie('area_code');
    $data['user_img'] = cookie('user_img');
    $data['roleid'] = cookie('roleid');
    $data['current_role'] = cookie('current_role');
    $data['current_role_name'] = cookie('current_role_name');
    return $data;
}

/**
 * 获取当前登录用户信息
 * 
 * @return NULL
 */
function get_user_info()
{
    $data['userid'] = cookie('userid');
    $data['user_name'] = cookie('user_name');
    $data['user_real_name'] = cookie('user_real_name');
    return $data;
}

/**
 * 判断用户是否登录
 * 
 * @return boolean
 */
function is_login()
{
    $model = new CommonModel();
    $userid = $model->getUserId();
    if (! empty($userid)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 获取用户角色
 * 
 * @param string $userid            
 * @return string
 */
function get_user_role($userid)
{
    $role_list = array();
    // 查询当前用户已授权的角色
    $model = new UserRoleModel();
    $roleid = $model->where("userid = '" . $userid . "'")->getField('roleid', true);
    return $roleid;
}

/**
 * 获取数据字典列表
 * 
 * @param string $table_name            
 * @param string $field_name            
 * @return array
 */
function set_field_dict_list($table_name, $field_name, $remark = '')
{
    $model = new FieldDictModel();
    $data = $model->field("field_value as id,
							   field_mean as name,
							   remark,
							   seq")
        ->where("table_name = '" . $table_name . "'
						and field_name = '" . $field_name . "'
						and status = 'a'" . ($remark == '' ? '' : " and remark = '" . $remark . "'"))
        ->order('seq')
        ->select();
    return $data;
}

/**
 * 获取数据字典列表
 * 
 * @param string $name
 *            系统选项名称
 * @return string 系统选项值
 */
function get_system_option($name)
{
    $model = new SystemOptionModel();
    $data = $model->where("name = '" . $name . "'")->getField("value");
    return $data;
}

/**
 * 获取默认地域代码
 */
function get_default_area_code()
{
    $type = cookie('type');
    if ($type == 'a') {
        $area_code = get_system_option('默认查询县域代码');
    } else {
        $area_code = cookie('area_code');
    }
    if (strlen($area_code) != 6) {
        $model = new SchoolModel();
        $area_code = $model->where("area_code like '" . $area_code . "%'")->getField("area_code");
    }
    return $area_code;
}

function get_nation_list()
{
    $model = new NationModel();
    $data = $model->field(id, name)->select();
    return $data;
}

/**
 * 获取省份列表
 * 
 * @return array
 */
function get_province_list()
{
    $model = new ProvinceModel();
    $data = $model->field("code, name")
        ->where("code in ('62','36','42','53','33','37')")
        ->select();
    
    // $data = $model->field("code, name")->where("code in (select substring(area_code, 1, 2) from school)")->select();
    
    return $data;
}

/**
 * 根据省份编码获取市州列表
 * 
 * @param string $province_code            
 * @return array
 */
function get_city_list($province_code)
{
    $model = new CityModel();
    $data = $model->field("code, name")
        ->where("province_code = '" . $province_code . "'")
        ->select();
    return $data;
}

/**
 * 根据市州编码获取县区列表
 * 
 * @param string $city_code            
 * @return string
 */
function get_area_list($city_code)
{
    $model = new AreaModel();
    $data = $model->field("code, name")
        ->where("city_code = '" . $city_code . "'")
        ->select();
    return $data;
}

/**
 * 根据县区编码获取学校列表
 * 
 * @param string $area_code            
 * @return string
 */
function get_school_list($area_code)
{
    $model = new SchoolModel();
    $data = $model->field("id code, name")
        ->where("area_code = '" . $area_code . "'")
        ->select();
    return $data;
}

/**
 * 根据学院获取专业
 *
 * @param string $college
 * @return string
 */
function get_speciality_list($college)
{
	$model = new CollegeSpecialityModel();
	$data = $model->field("specialityid code, get_speciality_name(specialityid) name")
	->where("collegeid = '" . $college. "'")
	->select();
	return $data;
}

/**
 * 根据学院获取教师
 *
 * @param string $school
 * @param string $college
 * @param string $speciality
 * @return string
 */
function get_teacher_list($school,$college,$speciality)
{
	$model = new GroupsUserModel();
	$data = $model->field("id code, real_name name")
	->where("school = '" . $school. "' and college = '" . $college. "' and speciality = '" . $speciality. "' and type='b'")
	->select();
	//echo $model->getLastSql();
	//exit;
	return $data;
}

function get_teachers_list($college)
{
	$model = new GroupsUserModel();
	$data = $model->field("id code, real_name name")
	->where("college = '" . $college. "'  and type='b'")
	->select();
	//echo $model->getLastSql();
	//exit;
	return $data;
}
/**
 * 根据学段获取学科列表
 * 
 * @param string $phase            
 * @return string
 */
function get_subject_list($phase)
{
    switch ($phase) {
        case 'a':
            $subject = array(
                array(
                    'id' => '1',
                    'name' => '品德与生活品德与社会'
                ),
                array(
                    'id' => '2',
                    'name' => '语文'
                ),
                array(
                    'id' => '3',
                    'name' => '数学'
                ),
                array(
                    'id' => '4',
                    'name' => '英语'
                ),
                array(
                    'id' => '5',
                    'name' => '音乐'
                ),
                array(
                    'id' => '6',
                    'name' => '美术'
                ),
                array(
                    'id' => '7',
                    'name' => '科学'
                ),
                array(
                    'id' => '8',
                    'name' => '体育与健康'
                ),
                array(
                    'id' => '9',
                    'name' => '信息技术'
                )
            );
            break;
        case 'b':
            $subject = array(
                array(
                    'id' => '11',
                    'name' => '思想品德'
                ),
                array(
                    'id' => '12',
                    'name' => '历史与社会'
                ),
                array(
                    'id' => '13',
                    'name' => '语文'
                ),
                array(
                    'id' => '14',
                    'name' => '数学'
                ),
                array(
                    'id' => '15',
                    'name' => '外语'
                ),
                array(
                    'id' => '16',
                    'name' => '物理'
                ),
                array(
                    'id' => '17',
                    'name' => '化学'
                ),
                array(
                    'id' => '18',
                    'name' => '生物'
                ),
                array(
                    'id' => '19',
                    'name' => '历史'
                ),
                array(
                    'id' => '20',
                    'name' => '地理'
                ),
                array(
                    'id' => '21',
                    'name' => '音乐'
                ),
                array(
                    'id' => '22',
                    'name' => '美术'
                ),
                array(
                    'id' => '23',
                    'name' => '体育与健康'
                ),
                array(
                    'id' => '24',
                    'name' => '信息技术'
                )
            );
            break;
        case 'c':
            $subject = array(
                array(
                    'id' => '31',
                    'name' => '思想品德'
                ),
                array(
                    'id' => '32',
                    'name' => '历史与社会'
                ),
                array(
                    'id' => '33',
                    'name' => '语文'
                ),
                array(
                    'id' => '34',
                    'name' => '数学'
                ),
                array(
                    'id' => '35',
                    'name' => '外语'
                ),
                array(
                    'id' => '36',
                    'name' => '物理'
                ),
                array(
                    'id' => '37',
                    'name' => '化学'
                ),
                array(
                    'id' => '38',
                    'name' => '生物'
                ),
                array(
                    'id' => '39',
                    'name' => '历史'
                ),
                array(
                    'id' => '40',
                    'name' => '地理'
                ),
                array(
                    'id' => '41',
                    'name' => '音乐'
                ),
                array(
                    'id' => '42',
                    'name' => '美术'
                ),
                array(
                    'id' => '43',
                    'name' => '体育与健康'
                ),
                array(
                    'id' => '44',
                    'name' => '信息技术'
                )
            );
            break;
        default:
            $subject = array();
    }
    return $subject;
}

/**
 * 获取网站角色列表
 * 
 * @param string $webid            
 */
function get_system_role()
{
    $model = new RoleModel();
    $list = $model->field("id,
							   name")
        ->where("type = 'a'")
        ->order('seq')
        ->select();
    return $list;
}

/**
 * 获取角色名称
 * 
 * @param string $roleid            
 * @return string
 */
function get_role_name($roleid)
{
    $model = new RoleModel();
    $name = $model->where("id = '" . $roleid . "'")->getField('name');
    return $name;
}

/**
 * 删除指定地址下的文件
 * 
 * @param string $path
 *            文件路径
 */
function delFile($path)
{
    try {
        if (file_exists($path)) {
            unlink($path);
        }
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = false)
{
    if (function_exists("mb_substr")) {
        if ($suffix)
            return mb_substr($str, $start, $length, $charset) . "...";
        else
            return mb_substr($str, $start, $length, $charset);
    } elseif (function_exists('iconv_substr')) {
        if ($suffix)
            return iconv_substr($str, $start, $length, $charset) . "...";
        else
            return iconv_substr($str, $start, $length, $charset);
    }
    $re['utf-8'] = "/[x01-x7f]|[xc2-xdf][x80-xbf]|[xe0-xef][x80-xbf]{2}|[xf0-xff][x80-xbf]{3}/";
    $re['gb2312'] = "/[x01-x7f]|[xb0-xf7][xa0-xfe]/";
    $re['gbk'] = "/[x01-x7f]|[x81-xfe][x40-xfe]/";
    $re['big5'] = "/[x01-x7f]|[x81-xfe]([x40-x7e]|xa1-xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    if ($suffix)
        return $slice . "…";
    return $slice;
}

/**
 * 将文件大小换算成合适的单位
 * 
 * @param int $size            
 * @return $size
 */
function convertSize($size)
{
    // Adapted from: http://www.php.net/manual/en/function.filesize.php
    $mod = 1024;
    $units = explode(' ', 'B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i ++) {
        $size /= $mod;
    }
    return round($size, 2) . ' ' . $units[$i];
}

/* ============权限控制代码开始============ */

/**
 * 判断用户是否为超管（改写法考虑到扩展性，所以不直接通过cookie判断）
 * 
 * @param unknown $userid            
 * @return boolean
 */
function is_super_admin($userid)
{
    $model = new UserModel();
    $user_name = $model->where("id = '" . $userid . "'")->getField('name');
    if ($user_name == 'admin') {
        return true;
    } else {
        return false;
    }
}

/**
 * 取二维数字中的一列做为一维数组
 * 
 * @param unknown_type $input            
 * @param unknown_type $columnKey            
 * @param unknown_type $indexKey            
 * @return Ambigous <string, NULL, mixed>
 */
function i_array_column($input, $columnKey, $indexKey = null)
{
    if (! function_exists('array_column')) {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber = (is_numeric($indexKey)) ? true : false;
        $result = array();
        foreach ((array) $input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && ! empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? "'" . $row[$columnKey] . "'" : null;
            }
            if (! $indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && ! empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    } else {
        return array_column($input, $columnKey, $indexKey);
    }
}
function set_mgc($text){
	Vendor('text.AipImageCensor');
	//你的 APPID AK SK
	$APP_ID = '21536974';
	$API_KEY = 'eP7SPOhabzSWK0GTV7oD7gnv';
	$SECRET_KEY = 'atNKGtOsKarSFfKsdkyxbcasTkuSE1cY';
	$client = new \AipImageCensor($APP_ID, $API_KEY, $SECRET_KEY);
	$result = $client->textCensorUserDefined($text);
	if($result['conclusion'] == '合规'){
		$data=array();
	}else{
		foreach($result['data'] as $key=>$value){
			$data[$key] = $value['msg'];
		}
	}
	$list ='';
	$list = implode(",",$data);
	return $list;
}
?>